destination_path = './photo/'
def try_loop(fun):
    while True:
        try:
            fun()
            break
        except Exception as e:
            print(e)
            time.sleep(1)

def on_light():
    requests.get("http://192.168.8.143/on/")
    time.sleep(4)

def off_light():
    requests.get("http://192.168.8.143/off/")


def get_photo():
    r = requests.get("http://192.168.1.143/capture")
    with open("{}{}.jpg".format(destination_path,datetime.now().strftime("%Y%m%d_%H%M%S")),"wb") as f:
        f.write(r.content)


def job():
    print('take photo')
    try_loop(on_light)
    try_loop(get_photo)
    try_loop(off_light)

#schedule.every(1).minutes.do(job)
schedule.every(10).seconds.do(job)
#schedule.every(30).seconds.do(job)
while True:
    schedule.run_pending()
    time.sleep(1)
